<?php
/**
 * 集团逻辑层
 */
namespace AuctionBack\Logic;
use AuctionCore\Account\Back\Department;
class BlocLogic {

    Protected $autoCheckFields = false;
    
    /**
     * 集团ID
     */
    private $bid;

    /**
     * 构造,初始化"集团ID"
     */
    public function __construct($bid)
    {
        $this->bid = $bid;
        $this->model = D("Bloc","Model");
    }

   /**
     * 新增
     */
    public static function create($data)
    {   
        $data['createtime'] =  date("Y-m-d H:i:s", time());
        $res = D("Bloc","Model") ->add($data);
    }

    /**
     * 修改
     */
    public function update($data,$bid)
    {
        if ($bid < 1) return false;
        $data['modifytime'] =  date("Y-m-d H:i:s", time());
        return $this->model->where(['bid' => $bid])->save($data);
    }

    /**
     * 列表
     */
    public static function getBlocList($page, $limit, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $limit = (int)$limit < 1 ? 10 : (int)$limit;
        //搜索 
        if ($params['b_name']) {
            $map['b_name'] = array('eq',$params['b_name']);
        }
        if ($params['b_phone']) {
            $map['phone'] = array('eq',$params['b_phone']);
        }
        if ($params['b_contact']) {
            $map['contact'] = array('eq',$params['b_contact']);
        }
        if ($params['branch_id'] > 0) {
            $map['branch_id'] = array('eq',$params['branch_id']);
        }
        $total = D('Bloc','Model')->where($map)->count();
        $list = D('Bloc','Model')->where($map)->limit($limit)->page($page)->order('bid desc')->select();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    } 

    /**
     * 集团名称检查
     */
    public function checkBlocName($blocName,$bid = 0)
    {   
        $map = [];
        if ($bid > 0) {
            $map['bid'] = ['neq',$bid];
        }
        $map['b_name'] = ['eq',$blocName];
        return $bloc = $this->model->where($map)->find();
    }


    /**
    *集团信息
    */
    public function getBlocInfo($bid)
    {
        if($bid < 1) return false;
        $map = [];
        $map['bid'] = ['eq',$bid];
        $blocInfo = $this->model->where($map)->find();
        //找到集团关联4s店信息
        $fourStore = FourStoreLogic::getFourStoreByBid($bid);
        $blocInfo['fourStore'] = $fourStore;
        return $blocInfo;
    }
    /**
    *获取集团名称
    */
    public function getBnameByBid($bid)
    {
        if($bid < 1) return false;
        return $bName = $this->model->where(['bid'=>$bid])->getField('b_name');
    }

    /**
    * 关联4s店提交
    */
    public function blocRelationFourStorePost($bid,$fids)
    {   
        $FourStore = D('FourStore','Logic');
        $fourStoreInfos = $FourStore->getInFourStoreByBid($bid);
        $fourStoreIds = [];
        foreach ($fourStoreInfos as $key => $value) {
            $fourStoreIds[] = $value['fid'];
        }
        if(empty($fids)){
            if (empty($fourStoreIds)) return true; 
            $res = $FourStore->delRelationBloc($fourStoreIds);
            if (!$res) return false; 
        }else{
            $add_spm = array_diff($fids, $fourStoreIds);
            $del_spm = array_diff($fourStoreIds, $fids);
            if (!empty($add_spm)) {
                $res1 = $FourStore->addRelationBloc($add_spm,$bid);
                if (!$res1) return false; 
            }
            if (!empty($del_spm)) {
                $res2 = $FourStore->delRelationBloc($del_spm);
                if (!$res2) return false; 
            }
        }
        return true;
    }
}